package com.jingyanzi.sys.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.sys.dao.SysNoticeMapper;
import com.jingyanzi.sys.domain.NoticeDO;
import com.jingyanzi.sys.domain.query.NoticeQueryDO;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.INoticeService;
import com.jingyanzi.sys.entity.SysNotice;
import com.jingyanzi.sys.entity.SysNoticeExample;
import com.jingyanzi.sys.entity.SysNoticeExample.Criteria;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("iNoticeService")
public class NoticeServiceImpl extends BaseServiceImpl implements INoticeService {
	
	@Autowired
	private SysNoticeMapper noticeMapper;

	@Override
	public ResultDO add(NoticeDO noticeDO) {
		
		SysNotice notice = new SysNotice();
		
		ResultSupport result = BeanUtilsExtends.copy(notice, noticeDO);
		
		if (!result.isSuccess()) {
			return result;
		}

		notice.setCreatetime(Calendar.getInstance().getTime());
		notice.setModifitime(notice.getCreatetime());
		notice.setIsDelete(1);
		notice.setIsVisible(1);
		int r = -1;
		
		r = noticeMapper.insert(notice);
		if (r < 1) {
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, notice.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer id) {
		
		ResultSupport result = new ResultSupport();
		
		SysNotice sysNotice = new SysNotice();
		
		sysNotice.setId(id);
		sysNotice.setIsDelete(-1);
		
		int r = -1;

		try {
        	r = noticeMapper.updateByPrimaryKeySelective(sysNotice);
        } catch(Exception e) {
        	
        }

        if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO modifi(NoticeDO noticeDO) {
		
		SysNotice notice = new SysNotice();
		
		ResultSupport result = BeanUtilsExtends.copy(notice, noticeDO);
        if (!result.isSuccess())
        	return result;
       
        int r = -1;
        try {
            r = noticeMapper.updateByPrimaryKeySelective(notice);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        if (r < 1) {
        	result.setSuccess(false);
        }
       
        return result;
	}

	@Override
	public ResultDO get(Integer id) {
		
		ResultSupport result = new ResultSupport();
		
		if (id < 1) {
			result.setSuccess(false);
			return result;
		}
		
		SysNotice notice = null;
		
		try {
			notice = noticeMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		
		if(notice == null) {
			result.setSuccess(false);
		}
		
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getNoticeDO(notice));
		
		return result;
	}

	@Override
	public ResultDO query(NoticeQueryDO query) {
		
		ResultSupport result = new ResultSupport();
		
		List<SysNotice> list = null;
		
		SysNoticeExample example = new SysNoticeExample();
		Criteria createCriteria = example.createCriteria();
		if(StringUtils.isNotBlank(query.getTitle())) {
			createCriteria
				.andTitleLike("%" + query.getTitle() + "%");	
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<NoticeDO> noticeDOList = null;		
		
		
		if(query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = noticeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		
		try {
			list = noticeMapper.selectByExample(example);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		
		noticeDOList = getNoticeDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, noticeDOList);

		return result;
	}

}
